//
//  CommitViewController.m
//  Findif
//
//  Created by Mac on 2019/1/10.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "CommitViewController.h"
#import "CommitTableViewCell.h"
#import "CommitModel.h"
#import "QuestionDetailViewController.h"

@interface CommitViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray <CommitModel *> * commitArray;

@end

@implementation CommitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = ZRWhiteColor;
    self.title = @"常见问题";
    
    [self loadData];
    [self setupUI];
}


- (void)setCommitArray:(NSArray<CommitModel *> *)commitArray{
    
    _commitArray = commitArray;
    
    [self.tableView reloadData];
}

- (void)loadData{
    
    [[ZRNetworkTool sharedTool] getCommonProblemsListResultWithBlock:^(BOOL success, id responseData) {
        
//        NSLog(@"res===%@",responseData);
        if (success) {
            
            self.commitArray = [NSArray yy_modelArrayWithClass:[CommitModel class] json:responseData[@"data"]];
        }
    }];
}

- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ZRWhiteColor;//ColorWithRGB(0xF5F5F5);
    tableView.rowHeight = 50 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;
    
    self.tableView = tableView;
    
    
    [self.tableView registerClass:[CommitTableViewCell class] forCellReuseIdentifier:@"CommitTableViewCell"];
    
//    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//
//        [self updateData];
//    }];
    
    [self.view addSubview:self.tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    //    NSLog(@"=====%lu",(unsigned long)self.goodlistArray.count);
    return self.commitArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CommitTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommitTableViewCell" forIndexPath:indexPath];
    
    cell.model = self.commitArray[indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CommitModel * model = self.commitArray[indexPath.row];
    
    HiddenBar;
    
    QuestionDetailViewController * detailVC = [QuestionDetailViewController new];
    
    detailVC.question = model.question;
    detailVC.answer = model.answer;
    
    [self.navigationController pushViewController:detailVC animated:YES];
}

@end
